package cn.com.headfree.technician.dao.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.TSeniorTechnician;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.technician.dao.ISeniorDao;
import cn.com.headfree.vo.DealFileVo;


@Repository("seniorDao")
public class SeniorDaoImpl extends BaseDaoImpl<TSeniorTechnician> implements ISeniorDao {

	@Override
	public TOrgDept getOrgNameByOrgId(String baseOrgId) {
		StringBuilder sqlBuilder = new StringBuilder(
				"select name FROM t_org_dept ");
		sqlBuilder.append(" where id=? ");
		ArrayList<Object> arrayList = new ArrayList<Object>();
		arrayList.add(baseOrgId);
		return this.findUnique(TOrgDept.class, sqlBuilder.toString(), arrayList.toArray());
	}

	
	@Override
	public void addSenior(TSeniorTechnician senior) {
		String sql = "insert into t_senior_technician (id,erp_no,user_name,current_subj_level,acqu_time,ready_subj_level,year,org_id,org_name,edu_info,creater_id,creater_name,create_time,updater_id,updater_name,update_time,del_sign) values ";
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{erp_no,jdbcType=VARCHAR},")
				.append("#{user_name,jdbcType=VARCHAR},").append("#{current_subj_level,jdbcType=VARCHAR},")
				.append("#{acqu_time,jdbcType=DATE},").append("#{ready_subj_level,jdbcType=VARCHAR},")
				.append("#{year,jdbcType=VARCHAR},").append("#{org_id,jdbcType=VARCHAR},")
				.append("#{org_name,jdbcType=VARCHAR},").append("#{edu_info,jdbcType=VARCHAR},")
				.append("#{creater_id,jdbcType=VARCHAR},")
				.append("#{creater_name,jdbcType=VARCHAR},").append("#{create_time,jdbcType=DATE},")
				.append("#{updater_id,jdbcType=VARCHAR},").append("#{updater_name,jdbcType=VARCHAR},")
				.append("#{update_time,jdbcType=DATE},").append("#{del_sign,jdbcType=INTEGER})");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", senior.getId());
		params.put("erp_no", senior.getErpNo());
		params.put("user_name", senior.getUserName());
		params.put("current_subj_level", senior.getCurrentSubjLevel());
		params.put("acqu_time", senior.getAcquTime());
		params.put("ready_subj_level", senior.getReadySubjLevel());
		params.put("year", Integer.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		params.put("org_id", senior.getOrgId());
		params.put("org_name", senior.getOrgName());
		params.put("edu_info", senior.getEduInfo());
		params.put("creater_id", senior.getCreaterId());
		params.put("creater_name", senior.getCreaterName());
		params.put("create_time", new Timestamp(new Date().getTime()));
		params.put("updater_id", senior.getUpdaterId());
		params.put("updater_name", senior.getUpdaterName());
		params.put("update_time", new Timestamp(new Date().getTime()));
		params.put("del_sign", 0);
		this.executeSql(sqlBuilder.toString(), params);
	}


	@Override
	public void getPageSeniorTechnician(Page<TSeniorTechnician> page, TSeniorTechnician tSeniorTechnician, User user) {
		ArrayList<Object> arrayList = new ArrayList<Object>();
		String sql = "select id,erp_no,user_name,current_subj_level,acqu_time,ready_subj_level,year,org_id,org_name,GET_DIC('education',edu_info) as edu_info,create_time from t_senior_technician where del_sign = 0 " ;
		StringBuilder sqlBuilder = new StringBuilder(sql);
		sqlBuilder.append(" and erp_no=? ");
		arrayList.add(user.getFax());
        if(tSeniorTechnician!=null) {
			if (tSeniorTechnician.getYear()!=null) {
	            sqlBuilder.append(" AND year= ? ");
	            arrayList.add(tSeniorTechnician.getYear());
	        }
        }
        
        if(tSeniorTechnician!=null) {
	        if (!StringUtil.isNullOrEmpty(tSeniorTechnician.getReadySubjLevel())) {
	            sqlBuilder.append(" AND ready_subj_level like concat('%',?,'%') ");
	            arrayList.add(tSeniorTechnician.getReadySubjLevel());
	        }
        }
		sqlBuilder.append(" order by create_time desc ");
		this.findPageBySql(TSeniorTechnician.class, sqlBuilder.toString(), page, arrayList.toArray());
	}


	@Override
	public TSeniorTechnician judge(TSeniorTechnician senior, User user) {
		StringBuilder sqlBuilder = new StringBuilder(
				"select id FROM t_senior_technician where 1=1 ");
		ArrayList<Object> arrayList = new ArrayList<Object>();
		sqlBuilder.append(" and erp_no=? ");
		arrayList.add(user.getFax());
		sqlBuilder.append(" and current_subj_level=? ");
		arrayList.add(senior.getCurrentSubjLevel());
		sqlBuilder.append(" and acqu_time=? ");
		arrayList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(senior.getAcquTime()));
		sqlBuilder.append(" and ready_subj_level=? ");
		arrayList.add(senior.getReadySubjLevel());
		sqlBuilder.append(" and year=? ");
		arrayList.add(Integer.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		return this.findUnique(TSeniorTechnician.class, sqlBuilder.toString(), arrayList.toArray());
	}

	public List<DealFileVo> getFilesByBusinessId(String businessId,String businessNode) {
		StringBuilder sqlBuilder = new StringBuilder(
				"select business_id,file_id,file_name,business_node,business_type_id,order_num ");
		sqlBuilder.append(" from t_file where 1=1 ");
		ArrayList<Object> arrayList = new ArrayList<Object>();
		if (!StringUtil.isNullOrEmpty(businessId)) {
			sqlBuilder.append(" and business_id=?");
			arrayList.add(businessId);
		}
		if (!StringUtil.isNullOrEmpty(businessNode)) {
			sqlBuilder.append(" and business_node=?");
			arrayList.add(businessNode);
		}
		sqlBuilder.append(" order by order_num ");
		return this.findAllBySQL(DealFileVo.class, sqlBuilder.toString(), null, false,arrayList.toArray());
	}


}
